﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using NF_Mode;

namespace NF_Tools
{
    public partial class BF_Remark : UserControl
    {
        #region - 字段 -
        ToolTip tip = new ToolTip();
        public string commentId = "";
        #endregion 

        #region - 属性 -
        [Category("WXP"), DisplayName("headAppear")]
        public string Head_Appear 
        {
            set { picHeadAppear.ImageLocation = value; }
        }

        [Category("WXP"), DisplayName("nickName")]
        public string Nick_Name 
        {
            set { lblNickName.Text = value; }
        }

        [Category("WXP"), DisplayName("commentTime")]
        public string Comment_time 
        {
            set { lblReplyTime.Text = value; }
        }

        [Category("WXP"), DisplayName("userContent")]
        public string User_Content
        {
            set { lblUserComment.Text = value; }
        }

        [Category("WXP"), DisplayName("SiteContent")]
        public string Site_Content
        {
            set { lblSiteComment.Text = FilterDirty(value); }
        }

        [Category("WXP"), DisplayName("ReplyContent")]

        public string Reply_Content
        {
            get { return txtSiteComment.Text; }
        }
        #endregion

        public BF_Remark()
        {
            InitializeComponent();
            tip.SetToolTip(lblSiteComment, "双击修改商家回复内容");
        }

        #region - 事件 -
        public delegate void Send(object sender, EventArgs e);     //定义委托
        public event Send SendEvent;                               //定义事件

        /// <summary>
        /// 发起商家回复
        /// </summary>
        private void btnReply_Click(object sender, EventArgs e)
        {
            btnReply.Visible = false;
            txtSiteComment.Visible = true;
            btnSend.Visible = true;
        }

        /// <summary>
        /// 提交回复
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSend_Click(object sender, EventArgs e)
        {
            if (SendEvent != null)
                SendEvent(sender, new EventArgs());//把按钮自身作为参数传递
        }

        /// <summary>
        /// 动态控制用户控件大小
        /// </summary>
        private void tableLayoutPanel1_SizeChanged(object sender, EventArgs e)
        {
            this.Height = tableLayoutPanel1.Height + 2;
        }

        /// <summary>
        /// 编辑回复信息
        /// </summary>
        private void lblSiteComment_Click(object sender, EventArgs e)
        {
            if (!txtSiteComment.Visible)
            {
                txtSiteComment.Visible = true;
                btnSend.Visible = true;
            }
        }
        #endregion

        #region - 脏字过滤 -
        /// <summary>
        /// 脏字显示
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        private string FilterDirty(string text)
        {
            Dictionary<char, List<string>> dicList = new Dictionary<char, List<string>>();
            System.Text.StringBuilder sb = new System.Text.StringBuilder(text.Length);
            string filterText = "需要过滤的脏字 以|分开";//脏字 可根据自己的方式用分隔符

            string[] filterData = filterText.Split('|');
            foreach (var item in filterData)
            {
                char value = item[0];
                if (dicList.ContainsKey(value))
                    dicList[value].Add(item);
                else
                    dicList.Add(value, new List<string>() { item });
            }

            int count = text.Length;
            for (int i = 0; i < count; i++)
            {
                char word = text[i];
                if (dicList.ContainsKey(word))//如果在字典表中存在这个key
                {
                    int num = 0;//是否找到匹配的关键字  1找到0未找到
                    var data = dicList[word].OrderBy(g => g.Length);//把该key的字典集合按 字符数排序(方便下面从少往多截取字符串查找)

                    foreach (var wordbook in data)
                    {
                        if (i + wordbook.Length <= count)//如果需截取的字符串的索引小于总长度 则执行截取
                        {
                            string result = text.Substring(i, wordbook.Length);//根据关键字长度往后截取相同的字符数进行比较
                            if (result == wordbook)
                            {
                                num = 1;
                                sb.Append(GetString(result));
                                i = i + wordbook.Length - 1;//比较成功 同时改变i的索引
                                break;
                            }
                        }
                    }
                    if (num == 0)
                        sb.Append(word);
                }
                else
                    sb.Append(word);
            }
            return sb.ToString();
        }
        /// <summary>
        /// 替换星号
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private static string GetString(string value)
        {
            string starNum = string.Empty;
            for (int i = 0; i < value.Length; i++)
            {
                starNum += "*";
            }
            return starNum;
        }
        #endregion
    }
}
